clear
clear matrix
capture log close
version 10
set mem 2g
set more off


* 1. Matching vars
local vars="lgdpc lgdpd n_comp preA2Bstr_pnl i_ad i_reg i_safe i_sub i_var jdemo s_agri s_simple s_complex s_services"
local exact="preA2Bstr_pnl"


local l=4 //Number of covariates/ predictors for participation test

foreach D in D_t D_pcn D_pdn {

use "01_Third_party_status.dta"

* 2. Confounding factors: histogram by D
* a. continuous vars
*sutex, digits(2) //Summary stats in tex
local k="`D'"
probit `k' `vars'

* Probit model
probit `k' `vars'

*Prepare vars by group for pre-balance stats
foreach var of varlist `vars' {
forvalues i=0(1)1 {
gen `var'`i'=`var' if `k'==`i'
quietly su `var' if `k'==`i'
gen v_`var'`i'=r(Var)
}
}

predict ps_`k', p
label variable ps_`k' ""
label define `k' 0 "Principal party" 1 "Third party"
label values `k' `k'


save "Thirdpartystatus.dta", replace

* 2. Matching and ATT 

use "Thirdpartystatus.dta"
postfile att_results_`k'_PSM t att se using att_results_`k'_PSM, replace

forvalues i=1(1)4 {
clear 
use "Thirdpartystatus.dta"
* Outcome: trade A2B after panel ruling
	rename A2B_sflow_p`i'_pnl Y`i' 
	su Y`i'
	display "+++++ ATT for `k': `i' years after ruling++++++"
	nnmatch Y`i' `k' ps_`k', robust(1) tc(att) level(95) exact(`exact') keep(attmatch_`k'_`i'_PSM) replace
	local attse=e(se)
	matrix att=el(e(b),1,1)
	svmat att
	su att
	local att=r(mean)
	post att_results_`k'_PSM (`i') (`att') (`attse')
}
postclose att_results_`k'_PSM
clear

}


* Figure 1: Third parties versus principal
clear
use att_results_D_t_PSM.dta
gen ub=att+1.96*se
gen lb=att-1.96*se

twoway  (rspike ub lb t) (scatter att t, msymbol(circle) mcolor(black) yline(0, lpattern(dash)) ylabel(#15, alternate labsize(small)) ytitle("Effect on Sectoral Trading Volume (Million US-$)" "" "") xtitle("Years after Ruling") title("") legend(off))
graph save "Figure 1.gph", replace
graph export "Figure 1.pdf", replace

clear

* Figure 3
* Figure 3a: Pro complainant third parties versus neutral
use att_results_D_pcn_PSM.dta
gen ub=att+1.96*se
gen lb=att-1.96*se

twoway  (rspike ub lb t) (scatter att t, msymbol(circle) mcolor(black) yline(0, lpattern(dash)) ylabel(#15, alternate labsize(small)) ytitle("Effect on Sectoral Trading Volume (Million US-$)") xtitle("Years after Ruling") title("Pro Complainant Third Parties") legend(off))
graph save "Figure 3a.gph", replace
clear

* Figure 3b: Pro Defendant third parties versus pro defendant versus neutral
use att_results_D_pdn_PSM.dta
gen ub=att+1.96*se
gen lb=att-1.96*se

twoway  (rspike ub lb t) (scatter att t, msymbol(c�ircle) mcolor(black) yline(0, lpattern(dash)) ylabel(#15, alternate labsize(small)) xtitle("Years after Ruling") title("Pro Defendant Third Parties") legend(off))
graph save "Figure 3b.gph", replace
clear

graph combine "Figure 3a.gph" "Figure 3b.gph", ycommon row(1) 
graph export "Figure 3.pdf", replace


exit
